import { createRouter, createWebHashHistory } from 'vue-router'
import adminIndex from '@/adminCore/views/home/adminIndex.vue'
import nav from './nav'
import Store from '@adminCore/store.js'
// import Home from '@/adminCore/views/watch/index.vue'
const routes = [
  {
    path: '/',
    name: 'adminIndex',
    component: adminIndex,
    redirect:"/watch/index",
    meta: { title: '后台', requireAuth: true },
    children: nav.NewRouter().map((item) => {
      return {
        meta: { title: item.meta.title, requireAuth: true },
        path: "/" + item.path,
        name: item.name,
        component: () => import('@/adminCore/views/' + item.path),
      }
    })
  },
  ...nav.base,
  
]

const router = createRouter({
  history: createWebHashHistory(),
  
  routes,
}) 

router.beforeEach((to, from,next) => {
  // to: Route: 即将要进入的目标 路由对象
  // from: Route: 当前导航正要离开的路由  
  // next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。
  let isLogin = Store.state.isLogin.state;
  
  // let isLogin = false 
  if (to.meta.requireAuth) {
    if (isLogin == false || isLogin == null) { 
      next({ path: '/login' })
    } else {
      next()
    }
  } else {
    next()
  }
   if (to.meta.title) {
    document.title = to.meta.title||""
  }
})
export default router 